package com.laiyao.bean;

import com.spring.AOP.AopAnnotation.*;
import com.spring.AOP.AopInterface.JoinPoint;
import com.spring.AOP.AopInterface.ProceedJoinPoint;
import com.spring.springAnnotation.Component;

import java.time.LocalDateTime;

@Aspect
@Component
public class LaiyaoAspect {
    @Before("com.spring.AOP.AopAnnotation.NeedLog")
    public void logBefore(JoinPoint joinPoint) {
        String methodName = joinPoint.getMethod().getName();
        Object[] args = joinPoint.getArgs();
        System.out.printf("【前置通知】时间：%s，方法：%s，参数：%s%n",
                LocalDateTime.now(), methodName, args);
    }
    @After("com.spring.AOP.AopAnnotation.NeedLog")
    public void logAfter(JoinPoint joinPoint) {
        String methodName = joinPoint.getMethod().getName();
        System.out.printf("【后置通知】时间：%s，方法：%s 执行完成%n",
                LocalDateTime.now(), methodName);
    }
    @Around("com.spring.AOP.AopAnnotation.NeedLog")
    public Object logAround(ProceedJoinPoint joinPoint) throws Throwable {
        String methodName = joinPoint.getMethod().getName();
        Object[] args = joinPoint.getArgs();
        // 环绕通知前置处理
        System.out.printf("【环绕通知-前】时间：%s，准备执行方法：%s，参数：%s%n",LocalDateTime.now(), methodName, args);
        Object result = null;
        // 执行目标方法
        result = joinPoint.proceed();
        // 目标方法执行成功后的处理
        System.out.printf("【环绕通知-后置】方法：%s 执行成功，返回值：%s%n",methodName, result);
        return result;
    }
}